package httpclient;

import com.google.gson.Gson;
import org.apache.http.Consts;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientWithResponseHandler {

    @Test
    public void gbsExchangeRate() {
        BasicHttpClient client = new BasicHttpClient();
        String body = client.get("https://devwealth.investmaster.cn/wealth/api/exchange/gbsExchangeRate",
                new MapGenerator<String, String>().add("date", "2017-07-25").map(), null);
        System.out.println(body);
    }

    @Test
    public void userPosition() {
        byte[] bytes = new byte[]{53, -75, -79, -128, 59, -32, -18, 113, -16, -91, 121, 106, 35, 73, 33, -79, 6, -44,
                122, -38, -87, 50, 104, 45, -90, 41, -38, 22, -83, 123, -100, -61, 69, 66, -58, 111, 73, -35, 48, -55};

        BasicHttpClient client = new BasicHttpClient();
        String body = client.get("https://devwealth.investmaster.cn/wealth/api/fund/userPosition",
                new MapGenerator<String, String>().add("securityNo", "kangqi-SG").map(),
                new MapGenerator<String, String>().add("token", new String(bytes, Consts.UTF_8)).map());
        System.out.println(body);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void gson() {
        String response = "{\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"cur\": \"USD\",//币种\n" +
                "            \"cost\": 60.064921,//成本，均价\n" +
                "            \"fundCode\": \"FUND_USD_CHAINA.FDUS.WM\",//基金code\n" +
                "            \"fundUnits\": 1570.907086//基金持仓\n" +
                "        },\n" +
                "        {\n" +
                "            \"cur\": \"USD\",\n" +
                "            \"cost\": 7.17,\n" +
                "            \"fundCode\": \"FUND_USD_WENBAN.FDUS.WM\",\n" +
                "            \"fundUnits\": 21602.147777\n" +
                "        }\n" +
                "    ],\n" +
                "    \"meta\": {\n" +
                "        \"code\": 200\n" +
                "    }\n" +
                "}";

        HashMap map = new Gson().fromJson(response, HashMap.class);
        Map kv = new HashMap();
        List<Map> data = (List<Map>) map.get("data");
        data.forEach(i -> kv.put(i.get("fundCode"), i.get("fundUnits")));

        String[] symbols = new String[]{"FUND_USD_CHAINA.FDUS.WM", "FUND_USD_WENBAN.FDUS.WM", "KM"};
    }
}
